<style scoped lang="scss">
.elscrollbar {
  ::v-deep .el-scrollbar__wrap {
    overflow-x: hidden;
  }

}
</style>
<template>
  <div>

    <router-view></router-view>
  </div>
</template>
<script>


//只要id的
function filterMenuwithid(array=[]){
return array.filter(item=>item.id)
}
export default {
  data() {
    return {


    };
  },
  components: {
  },
  watch: {

  },
  mounted() {

    this.initByDialog();




  },
  methods: {
    async initByDialog() {




    },



    initByResouces() {

    },
    changePage(menu) {
      this.selectMenu(menu);

    },
    fixedScrollCssProxy() {
      this.$refs['mainContent'].$refs['navTabs'].fixedScrollCss();
    },
    openMenuProxy(menu) {
      console.log('this.$refs[\'leftMenus\']', this.$refs['leftMenus']);
      this.$refs['leftMenus'].openMenu(menu);
    },

    changeCurrentKey(menu) {
      console.log('点击tab 切换 active key', menu);
      this.activedKey = menu && menu.id;
      //localStorage.setItem('iframeActiveKey', this.activedKey);
    },
    removeIframe(index) {
      this.iframeList.splice(index, 1);
      console.log('最新的iframelist', this.iframeList);
      localStorage.setItem('iframelist', JSON.stringify(this.iframeList));
    },
    setIframeList(newList) {
      this.iframeList = newList;
      console.log('最新的iframelist', this.iframeList);
      localStorage.setItem('iframelist', JSON.stringify(this.iframeList));
    },
    /**
     * @desc 选择到叶结点 的菜单是 推送菜单到 iframelist
     * @params
     * @return
     */
    selectMenu(menu) {
      var one = lodashUtils.findOneInArray(
          this.iframeList,
          item => item.id == menu.id,
      );
      if (lodashUtils.lodashEmpty(one)) {
        //判断 能不能继续推动到 iframelist中
        if (this.iframeList.length >= 16) {
          alertFail('界面打开超过限制');
          return { success: false };
        }
        this.iframeList.push(menu);
        this.activedKey = menu && menu.id;
      } else {
        this.activedKey = menu && menu.id;
      }


      console.log('tab active activekey', this.activedKey);


      console.log('最新的iframelist', this.iframeList);
      localStorage.setItem('iframeActiveKey', this.activedKey);
      sessionStorage.setItem('default-tags', JSON.stringify(this.iframeList));


      this.$nextTick(() => {
        this.fixedScrollCssProxy();
      });

    },
    refreshToken() {
      return new Promise((resolve, reject) => {
        console.log('开始进入 refreshToken');
        var freshToken=authUtil.getRefreshToken({domain:getDomain()});
        console.log("-> get freshToken", freshToken);
        if (!freshToken) {
          resolve(false);
          return;
        }
        return refreshToken(freshToken).then((res) => {
          if (!res.data) {
            resolve(false);
            return;
          }
          let tokenInfo = res.data;
          authUtil.setToken(tokenInfo?.accessToken,{domain:getDomain()});
          authUtil.setRefreshToken(tokenInfo?.refreshToken,{domain:getDomain()});
          authUtil.setTokenUpdatedTime(dateFormatYYYYMMDDHHmmss(tokenInfo?.refreshTime),{domain:getDomain()});
          delete tokenInfo.pageNum;
          delete tokenInfo.pageSize;
          delete tokenInfo.tenantId;
          delete tokenInfo.tenantType;
          delete tokenInfo.userId;
          resolve(tokenInfo);
        }).catch(error => {
          console.error(error);
          resolve(false);
        });
      });
    },
    async setTimeoutForTokenWithCookie() {
    var self=this;

      let timeout=60 * 58 * 4
      //let timeout=5

      var tokenFreshTime = authUtil.getTokenUpdatedTime({domain:getDomain()});
      var diffTime=moment(tokenFreshTime).add(timeout, 's').diff(new Date(), 's');
      console.log('开始判断是否需要刷新（4个小时）',diffTime,'s','tokenFreshTime:',tokenFreshTime);
      if (tokenFreshTime &&  diffTime <= 0/*过了4个小时 直接刷新*/) {
        await this.refreshToken()
      }


      //刷新之后 1个小时检查一次是否需要刷新
      setInterval(()=>{
        var tokenFreshTime = authUtil.getTokenUpdatedTime({domain:getDomain()});
        var diffTime=moment(tokenFreshTime).add(timeout, 's').diff(new Date(), 's');
        console.log('【轮训】开始判断是否需要刷新（4个小时）',diffTime,'s','tokenFreshTime:tokenFreshTime',tokenFreshTime);
        if (tokenFreshTime && diffTime <= 0 /*过了4个小时 直接刷新*/) {
          self.refreshToken();
        }
      },60 * 60 * 1000 );
    },


  },

};
</script>
